/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.zhpan.bannerview.view;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.app.Context;

import java.util.List;

/**
 * 扩展了PageSlide的动画接口
 *
 * @since 2021-04-12
 */
public class CollPageSlide extends PageSlider implements PageSlider.PageChangedListener {

    private List<Component> mComponents;
    private PageTransformer mPageTransformer;

    /**
     * 此类为了适配PageSlide滑动监听参数问题
     *
     * @param context 参数
     */
    public CollPageSlide(Context context) {
        this(context, null);
    }

    /**
     * 此类为了适配PageSlide滑动监听参数问题
     *
     * @param context 参数
     * @param attrSet 参数
     */
    public CollPageSlide(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
        addPageChangedListener(this);
    }

    /**
     * 此类为了适配PageSlide滑动监听参数问题
     *
     * @param context 参数
     * @param attrSet 参数
     * @param styleName 参数
     */
    public CollPageSlide(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    @Override
    public void onPageSliding(int position, float offset, int offsetPixels) {
        if (mPageTransformer != null) {
            onPageScrolled(position, offset, offsetPixels);
        }
    }

    @Override
    public void onPageSlideStateChanged(int state) {

    }

    @Override
    public void onPageChosen(int i1) {
        // NOTHING
    }

    /**
     * 设置PageSlide所有子组件
     *
     * @param components 子组件集合
     */
    public void setPages(List<Component> components) {
        this.mComponents = components;
    }

    /**
     * 设置动画接口
     *
     * @param pageTransformer 动画接口实现类
     */
    public void setPageTransformer(PageTransformer pageTransformer) {
        this.mPageTransformer = pageTransformer;
    }

    private void onPageScrolled(int position, float offset, int offsetPixels) {
        onPageTransformer(position, offset, offsetPixels);
    }

    private void onPageTransformer(int position, float positionOffset, int offsetPixels) {
        if (offsetPixels > 0) {
            scrollLeft(position, positionOffset);
        } else {
            scrollRight(position, positionOffset);
        }
    }

    private void scrollRight(int position, float positionOffset) {
        if (position - 1 >= 0) {
            Component in = mComponents.get(position - 1);
            mPageTransformer.transformPage(in, -1 + positionOffset);
        }
        Component out = mComponents.get(position);
        mPageTransformer.transformPage(out, positionOffset);
    }

    private void scrollLeft(int position, float positionOffset) {
        Component in = mComponents.get(position + 1);
        Component out = mComponents.get(position);
        mPageTransformer.transformPage(in, 1 - positionOffset);
        mPageTransformer.transformPage(out, 0 - positionOffset);
    }

    /**
     * PageSlide的动画接口
     *
     * @since 2021-04-12
     */
    public interface PageTransformer {
        /**
         * 动画接口
         *
         * @param page 参数
         * @param position 参数
         */
        void transformPage(Component page, float position);
    }
}
